package butines.game2.entity;

import butines.core.GameImage;


public class Building extends Entity {

    public static final int[][] GRID_3_3_PLUS   = { { 0, 1, 0 }, { 1, 1, 1 }, { 0, 1, 0 } };
    public static final int[][] GRID_3_3_SQUARE = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
    public static final int[][] GRID_2_2_SQUARE = { { 1, 1 }, { 1, 1 } };
    
    private int[][] passableGrid;
    private int[][] buildableGrid;
    
    private GameImage healthy;
    private GameImage contructing;
    
    private int width;
    private int height;
    
    private Animation fireAnm = new Animation();
    private GameImage fireImg;
    private int x;
    private int y;
    
    
    public Building() {
        
    }
    
    public void updateFire(float dt) {
        if (life <= hitPoints * .4f) {
            GameImage[] frames; 
            if (buildableGrid.length >= 3) {
                frames = game.getImgBigFire();
            } else {
                frames = game.getImgSmallFire();
            }
            fireAnm.animate(dt, 0.06f, frames.length, true);
            fireImg = frames[fireAnm.getFrame()];
        } else {
            fireImg = null;
        }
    }
    
    public void update(float dt) {
        updateFire(dt);
    }
    
    
    @Override
    public void renderImage(float sx, float sy) {
        super.renderImage(sx, sy);
        if (fireImg != null) {
            float x = sx + width/2 - fireImg.getWidth() / 2;
            float y = sy + height/2 - fireImg.getHeight() / 2 - 8;
            drawer.image(fireImg, x, y);
        }
    }

    public void renderDebug(float sx, float sy) {
        drawer.fill();
        drawer.red().circle(sx, sy, 3);
        drawer.center().print(sx, sy- 5, "%d,%d", getGridX(), getGridY());
        drawer.draw();
        drawer.rect(sx, sy, width, height);
        
        if (fireImg != null) {
            float x = sx + width/2 - fireImg.getWidth() / 2;
            float y = sy + height/2 - fireImg.getHeight() / 2 - 8;
            drawer.draw().red().rect(x, y, fireImg.getWidth(), fireImg.getHeight());
        }
        
    }
    
    public void renderSelection(float sx, float sy) {
        if (selected) {
            drawer.color(game.getSelectionFillColor());
            drawer.fill();
            drawer.oval(sx, sy, width, height);
            drawer.color(game.getSelectionBorderColor());
            drawer.draw();
            drawer.oval(sx, sy, width, height);
        }
    }
    
    public void renderLifeBar(float sx, float sy) {
        if (selected) {
            float x = sx;
            float y = sy - (game.getHealthBarHeight() * 2);
            float lifeBarWidth = width;
            
            if (life > hitPoints * .4f) {
                drawer.color(game.getHealthBarHealthyFillColor());
            } else {
                drawer.color(game.getHealthBarDamagedFillColor());
            }
            
            drawer.fill();
            drawer.rect(x, y, (lifeBarWidth * life / hitPoints), game.getHealthBarHeight());
            
            drawer.color(game.getHealthBarBorderColor());
            drawer.draw();
            drawer.rect(x, y, lifeBarWidth, game.getHealthBarHeight());
        }
    }
    
    public int[][] getPassableGrid() {
        return passableGrid;
    }

    public void setPassableGrid(int[][] passableGrid) {
        this.passableGrid = passableGrid;
    }

    public int[][] getBuildableGrid() {
        return buildableGrid;
    }
    
    public void setBuildableGrid(int[][] buildableGrid) {
        this.buildableGrid = buildableGrid;
    }
    
    public void setSize(int w, int h) {
        width = w;
        height = h;
    }
    
    public void setSheetImage(GameImage sheetImage) {
        GameImage[] arr = sheetImage.split(width, height, 2);
        healthy = arr[0];
        contructing = arr[1];
    }

    public void useHealthyImage() {
        image = healthy;
    }
    
    public void useContructingImage() {
        image = contructing;
    }
    
    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
    
}

